package com.mswyt.utils.test.validated;

import com.mswyt.utils.base.MsIocUtils;
import com.mswyt.utils.base.MsReflexUtils;
import com.mswyt.utils.msValidation.annotation.MsCheckMapParams;
import com.mswyt.utils.msValidation.annotation.MsCheckParams;
import com.mswyt.utils.test.excel.MsExcelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ValidatedTestController
 * @Description:
 */
@RestController
@RequestMapping("/test/vaild/")
@Validated
public class ValidatedTestController {

    @GetMapping("/test33")
    public void test33() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method dynamicMethod = MsReflexUtils.getMethod(MsExcelService.class, "dynamic", List.class, List.class);
        dynamicMethod.invoke(MsIocUtils.getClassObj(MsExcelService.class), new ArrayList<>(), new ArrayList<>());
    }


    /**
     * 检测params中的必填参数，k1、arg、sex三个参数值是否为空，为空抛出异常
     **/
    @MsCheckMapParams(keys = {"k1", "arg", "sex"})
    public Object test1(@RequestBody Map<String, Object> params) {
        return 222;
    }

    /**
     * 检测除了List<String> list参数外，其他参数是否为空，有一个为空就抛出异常
     **/
    @MsCheckParams(excludeIndex = {2}, message = "参数不可为空")
    public Object test1(String a1, int a2, List<String> list, Double b) {
        return 222;
    }


    /**
     * 两个结合使用，优先处理@MsCheckParams，当所有参数不为null，在去匹配@MsCheckMapParams的key是否为空
     **/
    @MsCheckParams
    @MsCheckMapParams(keys = {"k1"})
    public Object test2(@RequestBody Map<String, Object> params, String a1) {

        return 222;
    }


}
